// pages/purchasing_order/purchasing_order.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fabricList: [], //面料
    auxiliaryFabricList: [], //辅料
    total: 0,
    list: '',
    fabricList: [],
    auxiliaryFabricList: [],
    id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id
    })
    var that = this
    app.req('purchasingOrderForm/' + options.id, '', 'GET', res => {
      console.log(res)

      for (let i in res.data.data.fabricList) {
        if (res.data.data.fabricList[i].fabricType === '面料') {
          app.globalData.fabricList.push(res.data.data.fabricList[i])
        } else {
          app.globalData.auxiliaryFabricList.push(res.data.data.fabricList[i])
        }
      }
      that.setData({
        list: res.data.data,
        fabricList: app.globalData.fabricList,
        auxiliaryFabricList: app.globalData.auxiliaryFabricList,
        total: res.data.data.orderPrice
      })
    })
  },
  addFabric: function(e) {
    wx.navigateTo({
      url: '/pages/addFabric/addFabric?type=' + e.currentTarget.dataset.type + '&i=false&isTui=true'
    })
  },
  edit: function(e) {
    wx.navigateTo({
      url: '/pages/order_det/order_det?id=' + e.currentTarget.dataset.id
    })
  },
  sub: function() {
    var that = this
    var fabricList = this.data.fabricList.concat(this.data.auxiliaryFabricList)
    wx.request({
      url: app.globalData.api + 'purchasingOrderForm/add',
      data: {
        id: this.data.id,
        fabricList: fabricList
      },
      header: {
        Authorization: wx.getStorageSync('token')
      },
      method: 'POST',
      success: function(res) {
        if (res.data.code == 200) {
          app.globalData.fabricList = []
          app.globalData.auxiliaryFabricList = []
          that.setData({
            fabricList: app.globalData.fabricList,
            auxiliaryFabricList: app.globalData.auxiliaryFabricList,
            total: 0.00,
            ordName: ''
          })
          wx.showToast({
            title: '提交成功',
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.errorMsg,
          })
        }
      }
    })
  },
  price(e) {
    console.log(e)
    switch (e.currentTarget.dataset.type) {
      case "面料":
        app.globalData.fabricList[e.currentTarget.dataset.index].unitPrice = e.detail.value
        break
      case "辅料":
        app.globalData.auxiliaryFabricList[e.currentTarget.dataset.index].unitPrice = e.detail.value
        break
    }
  },
  number(e) {
    console.log(e)
    switch (e.currentTarget.dataset.type) {
      case "面料":
        app.globalData.fabricList[e.currentTarget.dataset.index].buyNumber = e.detail.value
        break
      case "辅料":
        app.globalData.auxiliaryFabricList[e.currentTarget.dataset.index].buyNumber = e.detail.value
        break
    }
  },
  fPrice(e) {
    console.log(e)
    switch (e.currentTarget.dataset.type) {
      case "面料":
        app.globalData.fabricList[e.currentTarget.dataset.index].fabricPrice = e.detail.value
        break
      case "辅料":
        app.globalData.auxiliaryFabricList[e.currentTarget.dataset.index].fabricPrice = e.detail.value
        break
    }
    console.log(app.globalData.auxiliaryFabricList)
  },
  z_fPrice(e) {
    var fabricListPrice = 0,
      auxiliaryFabricListPrice = 0
    for (var i in app.globalData.fabricList) {
      if (app.globalData.fabricList[i].fabricPrice != undefined) {
        fabricListPrice += Number(app.globalData.fabricList[i].fabricPrice)
      }
    }
    for (var i in app.globalData.auxiliaryFabricList) {
      if (app.globalData.auxiliaryFabricList[i].fabricPrice != undefined) {
        auxiliaryFabricListPrice += Number(app.globalData.auxiliaryFabricList[i].fabricPrice)
      }
    }
    this.setData({
      total: fabricListPrice + auxiliaryFabricListPrice,
    })
  },
  dele(e) {
    let that = this;
    if (e.currentTarget.dataset.id == '') {
      switch (e.currentTarget.dataset.type) {
        case "面料":
          app.globalData.fabricList.splice(e.currentTarget.dataset.index, 1)
          break
        case "辅料":
          app.globalData.auxiliaryFabricList.splice(e.currentTarget.dataset.index, 1)
          break
      }
      that.setData({
        auxiliaryFabricList: app.globalData.auxiliaryFabricList,
        fabricList: app.globalData.fabricList
      })
    } else {
      wx.request({
        url: app.globalData.api + 'purchasingOrderForm/fabric/' + e.currentTarget.dataset.id,
        data: '',
        method: 'DELETE',
        header: {
          Authorization: wx.getStorageSync('token')
        },
        success(res) {
          if (res.data.code == 200) {
            // wx.showToast({
            //   title: res.data.msg,
            // })
            switch (e.currentTarget.dataset.type) {
              case "面料":
                app.globalData.fabricList.splice(e.currentTarget.dataset.index, 1)
                break
              case "辅料":
                app.globalData.auxiliaryFabricList.splice(e.currentTarget.dataset.index, 1)
                break
            }
            that.setData({
              auxiliaryFabricList: app.globalData.auxiliaryFabricList,
              fabricList: app.globalData.fabricList
            })
          } else {
            wx.showToast({
              title: res.data.errorMsg,
            })
          }
        }
      })
    }



  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // var fabricListPrice = 0,
    //   auxiliaryFabricListPrice = 0
    // for (var i in app.globalData.fabricList) {
    //   fabricListPrice += Number(app.globalData.fabricList[i].fabricPrice)
    // }
    // for (var i in app.globalData.auxiliaryFabricList) {
    //   auxiliaryFabricListPrice += Number(app.globalData.auxiliaryFabricList[i].auxiliaryFabricPrice)
    // }
    this.setData({
      fabricList: app.globalData.fabricList,
      auxiliaryFabricList: app.globalData.auxiliaryFabricList,
      // total: (fabricListPrice + auxiliaryFabricListPrice).toFixed(2),
      user: wx.getStorageSync('user')
    })
    console.log(app.globalData.fabricList)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    app.globalData.auxiliaryFabricList = []
    app.globalData.fabricList = []

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})